package cn.player.act;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import cn.player.R;
import cn.player.download.DownLoadHelper;
import cn.player.utils.VideoUtil;
import cn.player.entity.Mp4Video;
import cn.player.utils.Constant;
import cntv.player.core.CBoxP2P;
import cntv.player.core.CBoxP2P.OnP2PBufferListener;
import cntv.player.core.util.LogUtil;
import cntv.player.core.util.ToastUtil;
import cntv.player.core.util.Utils;
import cntv.player.engine.Const;
import cntv.player.engine.ThreadPool;
import cn.player.media.entity.PlayMode;
import cn.player.media.entity.VideoInfo;
import cn.player.media.util.ParseUtil;
import cn.player.media.util.PlayModeUtil;

public class DownloadActivity extends BaseActivity {
	
	
	private ImageView ivCornerTest;
	
	private Button btnDownloadVideo;
	private Button btnDownloadPause;
	private Button btnPlayVideo,btnPlayVideoWebService;
	private TextView tvDownloadProgress;
	
	
	private Button btnDownloadVideo2;
	private Button btnDownloadPause2;
	private Button btnPlayVideo2;
	private TextView tvDownloadProgress2;
	private TextView tvDownloadLiveTs;
	
	
	private String hlsUrl;
	private String m3u8Url;
	private String m3u8Path;
	private List<String> m3u8VideoUrlList;
	public int count;
	private String m3u8VideoUrl;
	private DownLoadHelper mDownloadManager;
	private DownloadHandler mDownloadHander;
	private String targetUrl;
	public boolean mDownLoadStart;
	public int mCurrentTs;
	private String m3u8FileName;
	private String videoTitle;
	private OnClickListener mOnClickListener;

	protected String pathVid;

	private List<Mp4Video> mp4Videos;

	private DownloadMp4Handler mDownloadMp4Hander;

	public int mCurrentSize; 

	public int mTotalSize;

	private Button btnMergeVideo;

	protected String mP2pUrl;
	protected String mP2pUrlPre;

	private List<String> liveTsList;

	private Button btnDecryptTs;

	protected static final String TAG = "zl"; 
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_download);
		
		findViews();
		
		setListeners();
		
		videoTitle = "videoName2";
		
		
		Bitmap cornerBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.corner_test_icon);
		ivCornerTest.setImageBitmap(getRoundedCornerBitmap(cornerBitmap, getResources().getDimensionPixelOffset(R.dimen.border_corner_radius)));  
	}

	private void findViews() {
		// TODO Auto-generated method stub
		ivCornerTest = (ImageView) findViewById(R.id.iv_corner_test);
		
		btnDownloadVideo = (Button) findViewById(R.id.btn_download_video);
		btnDownloadPause = (Button) findViewById(R.id.btn_download_pause);
		btnPlayVideo = (Button) findViewById(R.id.btn_play_video);
		btnPlayVideoWebService = (Button) findViewById(R.id.btn_play_video_webservice);
		btnMergeVideo = (Button) findViewById(R.id.btn_merge_mp4);
		tvDownloadProgress = (TextView) findViewById(R.id.tv_download_progress);
		
		
		btnDownloadVideo2 = (Button) findViewById(R.id.btn_download_video2);
		btnDownloadPause2 = (Button) findViewById(R.id.btn_download_pause2);
		btnPlayVideo2 = (Button) findViewById(R.id.btn_play_video2);
		tvDownloadProgress2 = (TextView) findViewById(R.id.tv_download_progress2);
		tvDownloadLiveTs = (TextView) findViewById(R.id.btn_download_live_ts);
		btnDecryptTs = (Button) findViewById(R.id.btn_decrypt_ts_file);
		
		
		
	} 

	private void setListeners() {
		// TODO Auto-generated method stub
		
		mOnClickListener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				switch (v.getId()) {
				case R.id.btn_download_video:
					
					count = 0;
//					hlsUrl = "http://asp.cntv.lxdns.com/asp/hls/main/0303000a/3/default/5a2fee07cb484962a79f5cb24e98b6a8/main.m3u8";
//					hlsUrl = "http://asp.cntv.lxdns.com/asp/hls/main/0303000a/3/default/8b1ddc6fd1da443b9fc04273cd8aba08/main.m3u8";
//					hlsUrl = "http://asp.cntv.lxdns.com/asp/hls/main/0303000a/3/default/5588057b-fa55-11e4-89a6-d29fa354baaa/main.m3u8?maxbr=850";
//					hlsUrl = "http://asp.cntv.lxdns.com/asp/hls/main/0303000a/3/default/f7f4fd4dcb1849b7a1a9c38efbefa929/main.m3u8?maxbr=850";//小朋友好奇
					hlsUrl = "http://asp.cntv.lxdns.com/asp/hls/main/0303000a/3/default/d6a10cf382884b89ac48d45c252b3636/main.m3u8?maxbr=850";//英拉渎职案
					
					initJsonData(hlsUrl, true);
					
					break;
				case R.id.btn_decrypt_ts_file:
					String sourceUrl = Utils.getCacheVideoPath(videoTitle + "/" + Const.PLAYER_MODE_HD); 
					final List<String> sourceTsList = new ArrayList<String>();
					File file = new File(sourceUrl);
					if(file.isDirectory()){
						for(File f : file.listFiles()){
							sourceTsList.add(f.getAbsolutePath());
						}
					}
					targetUrl = Utils.getCacheVideoPath(videoTitle + "/" + "abcdefg"); 
					
					mDownloadManager = DownLoadHelper.getInstance();
					mDownloadHander = new DownloadHandler(sourceTsList);
					ThreadPool.getInstance().execute(new Runnable() {
							
							@Override
							public void run() { 
								// TODO Auto-generated method stub
								mDownloadManager.decryptTsFile(sourceTsList, 0, mDownloadHander, targetUrl);
							}
						});
					
					break;
				case R.id.btn_download_pause:
					
					if(mDownLoadStart){
						mDownloadManager.pauseDownLoad();
						btnDownloadPause.setText("点击继续下载");
					}else{
						mDownloadManager.resumeDownLoad();
						btnDownloadPause.setText("点击暂停下载");
						
						targetUrl = Utils.getCacheVideoPath(videoTitle + "/" + Const.PLAYER_MODE_HD); 
						if(targetUrl == null){
							ToastUtil.showToast(getApplicationContext(), "sd卡未加载，不能缓存视频", Toast.LENGTH_SHORT);
							return;
						}
						ThreadPool.getInstance().execute(new Runnable() {
							
							@Override
							public void run() {
								// TODO Auto-generated method stub
								mDownloadManager.download(m3u8VideoUrlList, mCurrentTs + 1, mDownloadHander, targetUrl);
								
							}
						});
					}
					break;
				case R.id.btn_play_video:
					Intent playCacheIntent = new Intent(mContext,MediaPlayActivity.class); 
					VideoInfo videoInfo = new VideoInfo();
					//点播基本信息
					videoInfo.setPort(true);
					videoInfo.setTitle("本地视频播放_" + videoTitle);
					
					videoInfo.setFlag(Const.VOD);
					videoInfo.setRate(Const.PLAYER_MODE_HD);
					if(TextUtils.isEmpty(m3u8FileName)){
						m3u8FileName = "/850.m3u8";
					}
//					videoInfo.setDetailUrl("/storage/sdcard0/Download/cntvbbs/video/[新闻直播间]关注欧洲非法移民问题 “蛇头”牟暴利 跨海竟用“死亡之舟”/850.m3u8");
					videoInfo.setDetailUrl(Utils.getCacheVideoPath(videoTitle + "/" + Const.PLAYER_MODE_HD) + m3u8FileName);
//					videoInfo.setDetailUrl(Utils.getCacheVideoPath("h265test" + "/" + Const.PLAYER_MODE_TS) + "/h265.m3u8");//for h265 live test
					
					playCacheIntent.putExtra(VideoInfo.class.getName(), videoInfo);
					startActivity(playCacheIntent);
					overridePendingTransition(R.anim.base_push_right_in,R.anim.base_fade_out);
					break;
				case R.id.btn_play_video_webservice:
					Intent playCacheIntent2 = new Intent(mContext,MediaPlayActivity.class); 
					VideoInfo vInfo = new VideoInfo();
					//点播基本信息
					vInfo.setPort(true);
					vInfo.setTitle("本地视频播放_" + videoTitle);
					
					vInfo.setFlag(Const.VOD);
					vInfo.setRate(Const.PLAYER_MODE_HD);
					if(TextUtils.isEmpty(m3u8FileName)){
						m3u8FileName = "/850.m3u8";
					} 
					String suffix = Utils.getCacheVideoPath(videoTitle + "/" + Const.PLAYER_MODE_HD) + m3u8FileName;
//					String suffix = Utils.getCacheVideoPath(videoTitle + "/abedefg") + m3u8FileName;
//					String suffix = "/storage/sdcard0/Download/cntvbbs/video/123456/850.m3u8";
					vInfo.setDetailUrl("http://127.0.0.1:" + Constant.PORT + suffix);
					
					playCacheIntent2.putExtra(VideoInfo.class.getName(), vInfo);
					startActivity(playCacheIntent2);
					overridePendingTransition(R.anim.base_push_right_in,R.anim.base_fade_out);
					break;
				
				case R.id.btn_download_video2:
					
					count = 0;
					pathVid = "http://vdn.apps.cntv.cn/api/getHttpVideoInfo.do?pid=5a2fee07cb484962a79f5cb24e98b6a8";
					initJsonData(pathVid);
					
					break;
				case R.id.btn_download_pause2:
					
					break;
				
				case R.id.btn_play_video2:
					
					break;
				case R.id.btn_merge_mp4:
					
					List<String> videoPaths = new ArrayList<String>();
					targetUrl = Utils.getCacheVideoPath(videoTitle + "/mp4"); 
					File dirFile = new File(targetUrl);
					File[] files = dirFile.listFiles();
					for (int i = 0; i < files.length; i++) {
						if (files[i].isFile()) {// 删除子文件
							videoPaths.add(files[i].getPath());
						}
					}
					try {
						VideoUtil.appendVideo(targetUrl, videoTitle + ".mp4") ;
						
						LogUtil.i("zl", "MergeVideo.appendVideo success .....");
					} catch (Exception e) {
						// TODO: handle exception
						e.printStackTrace();
					}
					
					/*ThreadPool.getInstance().execute(new Runnable() {
						
						@Override
						public void run() {
							// TODO Auto-generated method stub
							ShortenExample.appendVideo(videoPaths, targetUrl + "/merger.mp4") ;
							
						}
					});*/
						
					break;
				case R.id.btn_download_live_ts:
					
					p2pBuffer("pa://cctv_p2p_hdh265");
					
					/*Intent liveIntentNormal2 = new Intent(DownloadActivity.this,MediaPlayActivity.class); 
					videoInfo = new VideoInfo();
					videoInfo.setPort(true);
					videoInfo.setFlag(Const.LIVE);
					videoInfo.setTitle("h265testTitle");
					videoInfo.setChannelId("h265");
					liveIntentNormal2.putExtra(VideoInfo.class.getName(), videoInfo);
					startActivity(liveIntentNormal2);
					overridePendingTransition(R.anim.base_push_right_in,R.anim.base_fade_out); */
					break;
				default:
					break;
				}
			}
		};
		
		btnDownloadVideo.setOnClickListener(mOnClickListener);
		btnPlayVideo.setOnClickListener(mOnClickListener);
		btnPlayVideoWebService.setOnClickListener(mOnClickListener);
		btnDownloadPause.setOnClickListener(mOnClickListener);
		btnDownloadVideo2.setOnClickListener(mOnClickListener);
		btnPlayVideo2.setOnClickListener(mOnClickListener);
		btnDownloadPause2.setOnClickListener(mOnClickListener);
		btnMergeVideo.setOnClickListener(mOnClickListener);
		tvDownloadLiveTs.setOnClickListener(mOnClickListener);
		btnDecryptTs.setOnClickListener(mOnClickListener);
	}
	
	@Override
	protected void initDataSuccess(String url, JSONObject response) {
		// TODO Auto-generated method stub
		super.initDataSuccess(url, response);
		try {
			if(url.equals(pathVid)){//下载Mp4视频
				JSONArray jsonArray = response.optJSONObject("video").optJSONArray("chapters");
				mp4Videos = ParseUtil.parseDataToCollection(jsonArray, Mp4Video.class);
				
				mDownloadManager = DownLoadHelper.getInstance();
				mDownloadMp4Hander = new DownloadMp4Handler();
				targetUrl = Utils.getCacheVideoPath(videoTitle + "/mp4"); 
				if(targetUrl == null){ 
					ToastUtil.showToast(getApplicationContext(), "sd卡未加载，不能缓存视频", Toast.LENGTH_SHORT);
					return;
				}
				ThreadPool.getInstance().execute(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						mDownloadManager.download(mp4Videos.get(count).getUrl(), mDownloadMp4Hander, targetUrl);
						
					}
				});
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	@Override
	protected void initDataSuccess(String url, String response) {
		// TODO Auto-generated method stub
		super.initDataSuccess(url, response);
		if(url.equals(hlsUrl)){//hlsUrl访问成功，
			SparseArray<PlayMode> playModeSparse = PlayModeUtil.getVodPlayModeSparse(mContext, hlsUrl, response);
			m3u8Url = playModeSparse.get(Const.PLAYER_MODE_HD).getPlayUrl(); 
			
			//访问码率地址，下载m3u8地址里面的ts文件
//			initJsonData(m3u8Url, true); 
			
			//通过Afinal自带的文件下载器下载m3u8文件
			m3u8FileName = m3u8Url.substring(m3u8Url.lastIndexOf("/"), m3u8Url.length()); 
			targetUrl = Utils.getCacheVideoPath(videoTitle + "/" + Const.PLAYER_MODE_HD) + m3u8FileName; 
			downloadFile(m3u8Url, targetUrl);  //成功下载后调用  initDataSuccess(String url, File file)方法
		}else if(url.equals(m3u8Url)){
			m3u8VideoUrlList = PlayModeUtil.getM3U8VideoUrlList(m3u8Url, response); 
			targetUrl = Utils.getCacheVideoPath(videoTitle + "/" + Const.PLAYER_MODE_HD); 
			if(targetUrl == null){
				ToastUtil.showToast(getApplicationContext(), "sd卡未加载，不能缓存视频", Toast.LENGTH_SHORT);
				return;
			}
			mDownloadManager = DownLoadHelper.getInstance();
			mDownloadHander = new DownloadHandler(m3u8VideoUrlList);
			
			ThreadPool.getInstance().execute(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					mDownloadManager.download(m3u8VideoUrlList, 0, mDownloadHander, targetUrl);
					
				}
			});
		}
	}
	
	/**
	 * 替换ts后缀名为 为指定后缀名称
	 * @param path
	 * @param suffix
	 */
	private void replaceSuffix(String path,String suffix){ 
		InputStream fis = null;
		OutputStream fos = null;
		try {
			fis = new FileInputStream(path);
			byte[] data = readInputStream(fis);
			String m3u8Info = new String(data,0,data.length);
			m3u8Info = m3u8Info.replace(".ts", suffix);
			
			fos = new FileOutputStream(path);
			fos.write(m3u8Info.getBytes()); 
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			try {
				if(fis != null){
					fis.close();
				}
				if(fos != null){
					fos.close();
				}
				
			} catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
		}
	}
	
	@Override
	protected void initDataSuccess(String url, File file) {
		// TODO Auto-generated method stub
		super.initDataSuccess(url, file);
		try {
			if(url.equals(m3u8Url)){//下载m3u8文件成功，并解析文件，然后获取ts文件列表
				m3u8Path = file.getPath();
				InputStream fis = new FileInputStream(file);
				byte[] data = readInputStream(fis);
				String m3u8Info = new String(data,0,data.length);
				m3u8VideoUrlList = PlayModeUtil.getM3U8VideoUrlList(m3u8Url, m3u8Info); 
				targetUrl = Utils.getCacheVideoPath(videoTitle + "/" + Const.PLAYER_MODE_HD); 
				if(targetUrl == null){
					ToastUtil.showToast(getApplicationContext(), "sd卡未加载，不能缓存视频", Toast.LENGTH_SHORT);
					return;
				}
				mDownloadManager = DownLoadHelper.getInstance();
				mDownloadHander = new DownloadHandler(m3u8VideoUrlList);
				
				ThreadPool.getInstance().execute(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						mDownloadManager.download(m3u8VideoUrlList, 0, mDownloadHander, targetUrl);
						
					}
				});
			}else if(url.equals(mP2pUrl)){
				InputStream fis = new FileInputStream(file);
				byte[] data = readInputStream(fis);
				String liveTsInfo = new String(data,0,data.length);
				liveTsList = PlayModeUtil.getM3U8VideoUrlList(mP2pUrlPre, liveTsInfo);  
				targetUrl = Utils.getCacheVideoPath("h265test" + "/" + Const.PLAYER_MODE_TS);
				if(targetUrl == null){
					ToastUtil.showToast(getApplicationContext(), "sd卡未加载，不能缓存视频", Toast.LENGTH_SHORT);
					return;
				}
				mDownloadManager = DownLoadHelper.getInstance();
				mDownloadHander = new DownloadHandler(liveTsList);
				
				ThreadPool.getInstance().execute(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						mDownloadManager.download(liveTsList, 0, mDownloadHander, targetUrl);
						
					}
				});
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	@Override
	protected void initDataFailed(String url, int errorNo, String strMsg) {
		// TODO Auto-generated method stub
		super.initDataFailed(url, errorNo, strMsg);
	}
	
	private void p2pBuffer(String playId){
		CBoxP2P mP2pPlugin = CBoxP2P.getInstance(mContext); 
		mP2pPlugin.play(playId); 
		mP2pPlugin.setOnP2PBufferListener(new OnP2PBufferListener() {
			
			@Override
			public void p2pBufferSuccess(String p2pUrl) {
				// TODO Auto-generated method stub
				LogUtil.i(TAG ,"p2pBufferSuccess start play fast....");
				LogUtil.i(TAG ,"p2pBufferSuccess p2pUrl = " + p2pUrl);
				mP2pUrl = p2pUrl;
				mP2pUrlPre = mP2pUrl.substring(0, 21);
				
				targetUrl = Utils.getCacheVideoPath("h265test" + "/" + Const.PLAYER_MODE_TS) + "/h265.m3u8"; 
				downloadFile(mP2pUrl, targetUrl);  //成功下载后调用  initDataSuccess(String url, File file)方法
				
//				normalPlay(p2pUrl);
			}
			
			@Override
			public void p2pBufferFailed(int errNum, String errInfo) {
				LogUtil.i(TAG,"p2pBufferFailed start play slow.... errInfo = " + errInfo + ",errNum = " + errNum);
			}
		});
	}
	
	
	public static byte[] readInputStream(InputStream is) throws Exception {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		byte[] buf = new byte[1024];
		int len = 0;
		while ((len = is.read(buf)) != -1) {
			baos.write(buf, 0, len);
		}
		byte[] data = baos.toByteArray();
		return data;
	}
	
	class DownloadHandler extends Handler {
		private int totalTsCount;
		private List<String> tsUrlList; 
		

		public DownloadHandler(List<String> tsUrlList) {
			super();
			this.tsUrlList = tsUrlList;
			if(tsUrlList != null){
				totalTsCount = tsUrlList.size();
			}
		}

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {
			case DownLoadHelper.MSG_DOWNLOAD_START:
				mDownLoadStart = true;
				int startTs = (Integer) msg.obj;
				if(startTs == 0){
					ToastUtil.showToast(getApplicationContext(), "开始下载....", Toast.LENGTH_SHORT);
				}
				break;
			case DownLoadHelper.MSG_DOWNLOAD_ING:
				mCurrentTs = (Integer) msg.obj;
				String progress = String.format(Locale.getDefault(), "%.1f",  mCurrentTs /(float)(tsUrlList.size())*100);
				tvDownloadProgress.setText("下载进度：" + progress + "%");
				break;
			case DownLoadHelper.MSG_DOWNLOAD_SUCCESS:
				mDownLoadStart = false;
				ToastUtil.showToast(getApplicationContext(), "下载成功", Toast.LENGTH_SHORT);
				tvDownloadProgress.setText("下载完成");
				
				
				replaceSuffix(m3u8Path, Constant.SUFFIX_VIDEO);
				
				break;
			case DownLoadHelper.MSG_DOWNLOAD_CANCEL:
				mDownLoadStart = false;
				break;

			case DownLoadHelper.MSG_DOWNLOAD_ERROR:
				mDownLoadStart = false;
				tvDownloadProgress.setText("下载失败");

				break;
			case DownLoadHelper.MSG_DOWNLOAD_PAUSE:
				mDownLoadStart = false;
//				tvDownloadProgress.setText("暂停中。。。。");

				break;

			default:
				break;
			}
		}
	}
	
	
	
	class DownloadMp4Handler extends Handler {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {
			case DownLoadHelper.MSG_DOWNLOAD_START:
				mDownLoadStart = true;
				mTotalSize = (Integer) msg.obj; 
				ToastUtil.showToast(getApplicationContext(), "开始下载,第" + (count + 1) + "段视频.", Toast.LENGTH_SHORT);
				break;
			case DownLoadHelper.MSG_DOWNLOAD_ING:
				mCurrentSize = (Integer) msg.obj; 
				String progress = String.format(Locale.getDefault(), "%.1f",  mCurrentSize /(float)(mTotalSize)*100);
				tvDownloadProgress2.setText("下载进度：" + progress + "%");
				break;
			case DownLoadHelper.MSG_DOWNLOAD_SUCCESS:
				mDownLoadStart = false;
				ToastUtil.showToast(getApplicationContext(), "下载成功", Toast.LENGTH_SHORT);
				tvDownloadProgress2.setText("第" + (count + 1) + "段视频下载完成.");
				
				count++;
				if(count >= mp4Videos.size()){
					return;
				}
				ThreadPool.getInstance().execute(new Runnable() {

					@Override
					public void run() {
						// TODO Auto-generated method stub
						mDownloadManager.download(mp4Videos.get(count).getUrl(),mDownloadMp4Hander, targetUrl);

					}
				});
				
				break;
			case DownLoadHelper.MSG_DOWNLOAD_CANCEL:
				mDownLoadStart = false;
				break;

			case DownLoadHelper.MSG_DOWNLOAD_ERROR:
				mDownLoadStart = false;
				tvDownloadProgress.setText("下载失败");

				break;
			case DownLoadHelper.MSG_DOWNLOAD_PAUSE:
				mDownLoadStart = false;
//				tvDownloadProgress.setText("暂停中。。。。");

				break;

			default:
				break;
			}
		}
	}
	
	
	
	
	/**
     * 获取圆角图片
     * @param bitmap 原图bitmap
     * @param roundRadius 圆角半径
     * @return
     */
   public Bitmap getRoundedCornerBitmap(Bitmap bitmap,float roundRadius){   
         
       Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);  
       Canvas canvas = new Canvas(output);  
  
       final int color = 0xff424242;  
       final Paint paint = new Paint();  
       final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());  
       final RectF rectF = new RectF(rect);  
  
       paint.setAntiAlias(true);  
       canvas.drawARGB(0, 0, 0, 0);  
       paint.setColor(color);  
       /** 
        * 画一个圆角矩形 
        * rectF: 矩形 
        * roundRadius 圆角在x轴上或y轴上的半径 
        */  
       canvas.drawRoundRect(rectF, roundRadius, roundRadius, paint);  
       //设置两张图片相交时的模式  
       //setXfermode前的是 dst 之后的是src  
       //在正常的情况下，在已有的图像上绘图将会在其上面添加一层新的形状。   
       //如果新的Paint是完全不透明的，那么它将完全遮挡住下面的Paint；  
       //PorterDuffXfermode就可以来解决这个问题  
       //canvas原有的图片 可以理解为背景 就是dst  
       //新画上去的图片 可以理解为前景 就是src  
//     paint.setXfermode(new PorterDuffXfermode(Mode.SRC_OUT));  
       paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  
       canvas.drawBitmap(bitmap, rect, rect, paint);  
  
       return output;  
   } 
	

}
